package com.panopset.gui;

import java.io.StringWriter;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

import com.panopset.app.panopset.Application;
import com.panopset.java.Version;

import static com.panopset.Util.x;

/**
 * Panopset list editor.
 *
 * @author Karl Dinwiddie
 *
 */
public final class PropertiesEditor extends AbstractTableEditor {

    /**
     * Value column header.
     */
    private String keyColumnHeader = "Key";

    /**
     * @param value
     *            New key column header.
     */
    public void setKeyColumnHeader(final String value) {
        keyColumnHeader = value;
    }

    /**
     * Test PropertiesEditor.
     *
     * @param s
     *            Not used.
     */
    public static void main(final String... s) {
        Application app = new Application() {
            @Override
            protected JPanel createMainPanel() {
                return new PropertiesEditor(
                        new DocumentNamer(PropertiesEditor.class,
                                "testProps.txt"))
                        .getPanel();
            }
        };
        app.setVisible(true);
    }

    /**
     * Use this constructor if you have no default list built in the code.
     *
     * @param documentNamer
     *            Document namer.
     */
    public PropertiesEditor(final DocumentNamer documentNamer) {
        super(documentNamer);
    }

    /**
     * @return Edited map as Properties.
     */
    @SuppressWarnings("unchecked")
    public Properties getProperties() {
        if (!isLoaded()) {
            throw new RuntimeException(getDocumentName()
                    + " " + x("was never loaded."));
        }
        Properties p = new Properties();
        for (Object o : getModel().getDataVector()) {
            Vector<Object> v = (Vector<Object>) o;
            if (v.size() == 2) {
                p.put(v.elementAt(0).toString(), v.elementAt(1).toString());
            }
        }
        return p;
    }

    @Override
    protected void doAdd() {
        getModel().addRow(new String[] {"", "" });
    }

    @Override
    protected DefaultTableModel createModel() {
        return new DefaultTableModel(0, 2) {
            @Override
            public String getColumnName(final int columnIndex) {
                switch (columnIndex) {
                case 0:
                    return keyColumnHeader;
                case 1:
                    return getValueColumnHeader();
                default:
                    return "undefined";
                }
            }

            /**
             * Serial version id.
             */
            private static final long serialVersionUID = Version.UNI_VERSION;
        };
    }

    @Override
    protected void load(final Vector<String> v) {
        getModel().getDataVector().clear();
        for (String s : v) {
            StringTokenizer st = new StringTokenizer(s, "=");
            if (st.countTokens() == 2) {
                getModel().addRow(
                        new String[] {st.nextToken(), st.nextToken() });
            }
        }
    }

    @Override
    protected Vector<String> save() {
        Vector<String> rtn = new Vector<String>();
        for (Object o : getModel().getDataVector()) {
            if (o != null && o instanceof Vector<?>) {
                Vector<?> v = (Vector<?>) o;
                if (!v.isEmpty()) {
                    if (v.size() == 2) {
                        StringWriter sw = new StringWriter();
                        sw.append(v.elementAt(0).toString());
                        sw.append("=");
                        sw.append(v.elementAt(1).toString());
                        rtn.add(sw.toString());
                    }
                }
            }
        }
        return rtn;
    }
}
